(function(){
    $(".back").click(function(){
        $("body,html").animate({scrollTop:0},500)
    })

    var shop=true,area=true;
    var shopid=0,areaid=0;
        $.ajax({
            type:"GET",
            url:"http://chst.vip:1234/api/getgsshop",
        }).then(res=>{
            shopList=res.result;
            let html=""
            shopList.forEach(function (item) { 
                html+=`
                    <li>
                        ${item.shopName}
                    </li>
                `
            })
            $(".shoptext").html(shopList[0].shopName)
            $(".shopList").html(html)

 
            $(".shopList").children("li").click(function () { 
                shopid=$(this).index();
                $(".shopList").css("height","0")
                shop=true;
                $(".shoptext").html($(this).html())

                fn(shopid,areaid)
             })

        })
        

        $.ajax({
            type:"GET",
            url:"http://chst.vip:1234/api/getgsshoparea",
        }).then(res=>{
            areaList=res.result;
            let html=""
            areaList.forEach(function (item) { 
                html+=`
                    <li>
                        ${item.areaName}
                    </li>
                `
            })
            $(".areatext").html(areaList[0].areaName.split("（")[0])
            $(".areaList").html(html)

            $(".areaList").children("li").click(function () { 
                areaid=$(this).index();
                $(".areaList").css("height","0")
                area=true;
                $(".areatext").html($(this).html().split("（")[0])
                fn(shopid,areaid)
             })
        })

        $(".shop").click(function(){
            if(shop&&!area){
                $(".shopList").css("height","auto")
                $(".areaList").css("height","0")
                shop=false;
                area=true
            }else if(area&&shop){
                $(".shopList").css("height","auto")
                shop=false;
            }
            else{
                $(".shopList").css("height","0")
                shop=true;
            }
        })

        $(".area").click(function(){
            if(area&&!shop){
                $(".areaList").css("height","auto")
                $(".shopList").css("height","0")
                shop=true;
                area=false;
            }else if(area&&shop){
                $(".areaList").css("height","auto")
                area=false;
            }else{
                $(".areaList").css("height","0")
                area=true;
            }
        })

        function fn(shopid,areaid){
            $.ajax({
                type:"GET",
                url:"http://chst.vip:1234/api/getgsproduct",
                data:{shopid:shopid,areaid:areaid}
            }).then(res=>{
                let html=""
                res.result.forEach(function(item){
                    html+=`<li>
                        <img src="${item.productImg}">
                        <p>${item.productName}</p>
                        <p>${item.productPrice} <a>去凑单</a></p>
                        </li>
                    `
                })
                $(".box").html(html)
            })
        }
        fn(shopid,areaid)

})()